var express = require('express')
var app = express()
var router = express.Router()
var connection = require('../db/sql')
const path = require('path');

router.use(function timeLog(req, res, next) {
  // console.log('Time: ', Date.now())
  next()
})
// 查询用户接口
router.get('/user/list', function (req, res, next) {
  let searchName = req.query.searchName
  if (searchName) {
    connection.query('select * from user where name like "%' + searchName + '%"', function (error, results) {
      if (error) {
        console.log(error);
        return
      }
      res.send({
        data: results,
        status: 200,
        msg: '操作成功'
      })
    })
  } else {
    connection.query('select * from user', (error, results) => {
      if (error) {
        console.log(error);
        return
      }
      res.send({
        data: results,
        status: 200,
        msg: '操作成功'
      })
    })
  }
})
router.get('/index.html', function (req, res) {
  console.log(path.join(__dirname,'../pages','index.html'));
  res.sendFile(path.join(__dirname,'../pages','index.html'),  );
})

// 增加用户接口
router.post('/user/addUser', function (req, res, next) {
  let response = req.body
  // console.log(response);
  let addSql = `INSERT INTO user (name, age, money, password) VALUES (\'${response.name}\',${response.age},\'${response.money}\',\'${response.password}\')`
  //添加用户前判断是否存在同名
  let searchNameSql = 'select * from user where name ="' + response.name + '"'
  console.log(searchNameSql);
  connection.query(searchNameSql, (error, results) => {
    if (error) {
      console.log(error.message);
      return
    }
    console.log(results);
    if (results.length > 0) {
      res.send({
        code: -1,
        data: '存在相同名字'
      })
    } else {
      connection.query(addSql, (error, results) => {
        if (error) {
          console.log(error);
          return
        }
        res.send({
          code: 0,
          data: response
        })
      })
    }
  })


})
// 删除用户接口
router.get('/user/delUser', function (req, res, next) {
  let uid = req.query.uid
  if (uid) {
    let delSql = 'DELETE FROM user where id=' + uid;
    connection.query(delSql, function (err, results) {
      if (err) {
        console.log('[DELETE ERROR] - ', err.message);
        return
      }
      res.send({
        code: 0,
        data: '删除成功'
      })
    })
  }
})
// 修改用户接口
// router.post('/user/editUser', function (req, res, next) {
//   let uid = req.query.uid
//   let bodyParser =
//   if (uid) {
//     let editSql = 'UPDATE FROM user where id=' + uid;
//     connection.query(editSql, function (err, results) { 
//       if (err) { 
//         res.send({
//           code: 0,
//           data: 'error',
//           status:500
//         })
//         return
//       }
//       res.send({
//         code: 0,
//         data: '修改成功',
//         status:200
//       })
//     })
//   }
// })

router.get('/getData', (req, res) => {
  res.send('你好')
})
router.get('/getinfo/:id/:name', (req, res) => {
  console.log(req.params);
  // res.setHeader("Content-type","text/html;charset=utf8")
  res.send(`你好${req.params.name + req.params.id}`)
})
router.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})
module.exports = router